package com.dr.controller;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.dr.common.ValueUtil;

import com.dr.pojo.Invoice;
import com.dr.pojo.RegLevel;
import com.dr.pojo.Register;
import com.dr.pojo.Scheduling;
import com.dr.pojo.User;
import com.dr.service.InvoiceService;
import com.dr.service.RegLevelService;
import com.dr.service.RegisterService;
import com.dr.service.SchedulingService;



@Controller
@RequestMapping("/reg")
public class RegistController {
	@Autowired
	private RegLevelService regLevelService;
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private RegisterService registerService;
	@Autowired
	private SchedulingService SchedulingService;
	
	@RequestMapping("/regist")
	public String regist(Model md,Register regist) {
				
		//发票号
		Invoice invoice = invoiceService.nextInvoice();
		if(invoice != null) {
			md.addAttribute("invoice",invoice);		
		}else {
			md.addAttribute("invoice",new Invoice());
			md.addAttribute("errInvoice","发票已用完，请重新申请！");		
		}
		//病历号
		int caseNumber = registerService.nextCaseNumber();
		if(regist.getCaseNumber() == null) {
			md.addAttribute("caseNumber", caseNumber);				
		}else {
			md.addAttribute("caseNumber", regist.getCaseNumber());				
		}
			
		
		//挂号级别
		List<RegLevel> regLevList = regLevelService.findRegLevelList();
		md.addAttribute("regLevList", regLevList);
		
		md.addAttribute("regist", regist);		
		return "registManage/regist";		
	}
	
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md,String caseNumber) {
		if (ValueUtil.isEmpty(caseNumber)) {
			caseNumber = "";
		}
		
		Register regist = registerService.findRegistByCaseNumber(caseNumber);
		if(regist == null) {
			regist = new Register();
		}else {
			regist.setRegistLeID(0); //	查询病例信息时，不需要保持查询医生用的挂号级别
			regist.setDeptID(0);//	查询病例信息时，不需要保持查询医生用的部门名称
		}
		
		return regist(md, regist);		
	}	 
	
    
	//查询排班医生
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md, Register regist) {
		System.out.println(regist);
		List<Scheduling> schedulingList = SchedulingService.findSchedulingList(regist);
		md.addAttribute("schedulingList", schedulingList);
		System.out.println(schedulingList);
		return regist(md, regist == null ? new Register():regist);		
	}
	
	//挂号
	@RequestMapping("/doRegist")
	public String doRegist(Model md, Register regist, HttpSession session,Integer userid) {
		User loginUser = (User)session.getAttribute("LoginUser");
		//设置挂号者ID
		regist.setRegisterID(loginUser.getId());
		//设置医生ID
		regist.setUserID(userid);
		int count = registerService.insertRegist(regist);
		if(count == 0) {
			md.addAttribute("errMsg", "挂号失败请联系管理员");
		}else {
			invoiceService.updateInvoice(regist.getUserID(), regist.getInvoiceNum());
		}
		//回到费用结算页面
		return "registManage/fee";		
	}
		
}
